package cn.edu.xjtu.nbrash.xbarsuite.utilities;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;
import android.widget.Toast;

import com.google.zxing.client.android.wifi.NetworkType;
import com.google.zxing.client.android.wifi.WifiConfigInfo;
public class WifiManage {

	public static List<WifiConfigInfo> Read() throws Exception {
		List<WifiConfigInfo> wifiInfos=new ArrayList<WifiConfigInfo>();
	
		Process process = null;
		DataOutputStream dataOutputStream = null;
		DataInputStream dataInputStream = null;
		StringBuffer wifiConf = new StringBuffer();
		try {
			process = Runtime.getRuntime().exec("su");
			dataOutputStream = new DataOutputStream(process.getOutputStream());
			dataInputStream = new DataInputStream(process.getInputStream());
			dataOutputStream
					.writeBytes("cat /data/misc/wifi/*.conf\n");
			dataOutputStream.writeBytes("exit\n");
			dataOutputStream.flush();
			InputStreamReader inputStreamReader = new InputStreamReader(
					dataInputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(
					inputStreamReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				wifiConf.append(line);
			}
			bufferedReader.close();
			inputStreamReader.close();
			process.waitFor();
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (dataOutputStream != null) {
					dataOutputStream.close();
				}
				if (dataInputStream != null) {
					dataInputStream.close();
				}
				process.destroy();
			} catch (Exception e) {
				throw e;
			}
		}	
	
		
		Pattern network = Pattern.compile("network=\\{([^\\}]+)\\}", Pattern.DOTALL);
        Matcher networkMatcher = network.matcher(wifiConf.toString() );
        Log.i("ReadWifiConfig", wifiConf.toString());
        while (networkMatcher.find() ) {
            String networkBlock = networkMatcher.group();
            Pattern ssid = Pattern.compile("ssid=\"([^\"]+)\"");
            Matcher ssidMatcher = ssid.matcher(networkBlock);
            
            if (ssidMatcher.find() ) {   
            	WifiConfigInfo wifiInfo=new WifiConfigInfo();
                wifiInfo.setSsid(ssidMatcher.group(1));
                Pattern psk = Pattern.compile("psk=\"([^\"]+)\"");
                Matcher pskMatcher = psk.matcher(networkBlock);
                Pattern eap = Pattern.compile("identity=\"([^\"]+)\"");
                Matcher eapMatcher = eap.matcher(networkBlock);
                Pattern password = Pattern.compile("password=\"([^\"]+)\"");
                Matcher pswdMather = password.matcher(networkBlock);
                Pattern wep = Pattern.compile("wep_key[0-3]=([^\"]+)");
                Matcher wepMatcher = wep.matcher(networkBlock);
                if (pskMatcher.find() ) {
                	wifiInfo.setType(NetworkType.WPA);
                    wifiInfo.setPreSharedKey(pskMatcher.group(1));
                } else if(eapMatcher.find()&&pswdMather.find()){
                	wifiInfo.setType(NetworkType.EAP);
                	wifiInfo.setIdentity(eapMatcher.group(1));
                	wifiInfo.setPasswd(pswdMather.group(1));
                }else if(wepMatcher.find()){
                	wifiInfo.setType(NetworkType.WEP);
                	wifiInfo.setPreSharedKey(wepMatcher.group(1));
                }else{ //表示没有加密
                	wifiInfo.setType(NetworkType.NO_PASSWORD);
                }        
                Log.i("ReadWifiConfig", wifiInfo.toString());
                wifiInfos.add(wifiInfo);
            }
            
        }

		return wifiInfos;
	}

}


